<?php

namespace App\Http\Controllers\Admin\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Facades\DeviceService;

class DeviceController extends Controller
{
    public function addType(Request $request)
    {
    	$rules = [
    		'type' => 'required|unique:device_types,type',
    	];
    	$this->validate($request, $rules);
    	DeviceService::addType($request->only(['type']));
    	return formatJsonResponse();
    }

    public function updateType(Request $request)
    {
    	$rules = [
    		'id' => 'required|exists:device_types,id',
    		'type' => 'required|unique:device_types,type,'.$request->id,
    	];
    	$this->validate($request, $rules);

    	DeviceService::updateType($request->id, $request->only(['type']));
    	return formatJsonResponse();
    }

    public function deleteType(Request $request)
    {
    	$rules = [
    		'id' => 'required|exists:device_types,id',
    	];
    	$this->validate($request, $rules);

    	DeviceService::deleteType($request->id);
    	return formatJsonResponse();
    }

    public function allType()
    {
        $deviceTypes = DeviceService::getALLType();
        return formatJsonResponse(['device_types' => $deviceTypes]);
    }
}
